package com.xudow.app.dynamicstate_old.data.server;

import com.jude.utils.JUtils;
import com.xudow.app.XApplication;

import org.apache.http.cookie.Cookie;

import java.io.IOException;
import java.util.List;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class HeaderInterceptors implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request.Builder builder = request.newBuilder();
        if (XApplication.getInstance().isLogin()){
            String cookieStr = "";
            List<Cookie> cookies =  XApplication.getInstance().getCookieStore().getCookies();
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("JSESSIONID"))
                    cookieStr+=cookie.getName()+"="+cookie.getValue();
            }
            builder.addHeader("Cookie",cookieStr);
            JUtils.Log("HeaderInterceptors","加上Cookie:"+cookieStr);
        }else {
            JUtils.Log("HeaderInterceptors","没有登录");
        }
        builder.addHeader("mobile", "true");
        return chain.proceed(builder.build());
    }
}